﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.ViewModel;

namespace BBInterfaceNET.ProjectExplorer.Model
{
    public enum NodeState { Normal, NotOnDisk, NotInProject }

    public class ProjectNodeBase:NotificationObject
    {
        private bool editing;
        private string name;
        private ProjectNodeBase parent;
        private NodeState state; 

        public ProjectNodeBase()
        {
            Children = new ObservableCollection<ProjectNodeBase>();
            state = NodeState.Normal;
        }

        public NodeState State
        {
            get { return state; }
            set
            {
                if (state != value)
                {
                    state = value;
                    RaisePropertyChanged(() => State);
                }
            }
        }
        //the name of the node (file name with extension or dir name)
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    RaisePropertyChanged(() => Name);
                }
            }
        }
        //the parent node (null for root nodes)
        public ProjectNodeBase Parent
        {
            get { return parent; }
            set
            {
                if (parent != value)
                {
                    parent = value;
                    RaisePropertyChanged(() => Parent);
                }
            }
        }
        //is the node in editing mode?
        public bool Editing
        {
            get { return editing; }
            set
            {
                if (editing != value)
                {
                    editing = value;
                    RaisePropertyChanged(() => Editing);
                }
            }
        }
        //the collection of child nodes (for project node and directory nodes)
        public ObservableCollection<ProjectNodeBase> Children { get; private set; }
        

        public string GetPath()
        {
            if (Parent != null)
            {
                return Parent.GetPath() + "\\" + Name;
            }
            return "";
        }
        public void AddNode(ProjectNodeBase childToAdd)
        {
            childToAdd.Parent = this;
            if (childToAdd is DirectoryNode)
            {
                int idx = -1;
                for (int i = 0; i < this.Children.Count; i++)
                {
                    if (this.Children[i] is DirectoryNode)
                    {
                        idx = i;
                    }
                }
                this.Children.Insert(idx + 1, childToAdd);
            }
            else
            {
                this.Children.Add(childToAdd);
            }
        }
    }
}
